﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using TouchUI.System.Mobile.Mvc;
using LinkedService.Mobile.ServiceEntities;
using LinkedIn.Mobile.Properties;
using LinkedService.Mobile;

namespace LinkedIn.Mobile
{
  /// <summary>
  /// A list item for the connectionList
  /// </summary>
    public class ConnectionListItem : ScrollListItem
    {
        public static int ItemHeight  = Common.Instance.MinimumItemHeight * Common.Instance.ScreenFactor;
        public static int ImageSize   = Common.Instance.MinimumItemHeight * Common.Instance.ScreenFactor;
        public static int Spacing     =  2 * Common.Instance.ScreenFactor;
        public static int HSpacing    = 10 * Common.Instance.ScreenFactor;

        private int row;
        private Person person;
        private ProfileImage pImage;
        private bool LoadImage = true;
        
      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="row"></param>
      /// <param name="person"></param>
        public ConnectionListItem(int row, Person person)
        {
          // We count from 1
            this.row = row + 1;
            this.person = person;
            this.ScreenFactor = Common.Instance.ScreenFactor;

            if (LoadImage && !string.IsNullOrEmpty(person.PictureUrl))
              pImage = ProfileImageLoader.Instance.Create(person.PictureUrl);
            else
              pImage = new ProfileImage() { Picture = Resources.icon_no_photo_40x40, Loaded = true };
            Height = ItemHeight;

            ContainedItem = person;
        }

        #region Public properties
        public string Id { get { return person.Id; } }
        public int Distance { get { return person.Distance; } }
        #endregion

        public override void Paint(Graphics g)
        {
            Brush fontBrush = Selected ? Common.Instance.HighTextBrush:Common.Instance.FontBrush;
            Pen pen         = Selected ? Common.Instance.HighTextPen2 : Common.Instance.Pen2;
            Color backcolor = Selected ? SystemColors.Highlight : Color.White;

          // draw the image
            if (pImage.Loaded)
              g.DrawImage(pImage.Picture, new Rectangle(Rectangle.Left /*+ ConnectionListItem.Spacing*/, Rectangle.Top /*+ ConnectionListItem.Spacing*/, ConnectionListItem.ImageSize, ConnectionListItem.ImageSize),
                                 new Rectangle(0, 0, pImage.Picture.Width, pImage.Picture.Height), GraphicsUnit.Pixel);
            int maxstringwidth = Rectangle.Width - ConnectionListItem.HSpacing - ConnectionListItem.ImageSize - (16 * ScreenFactor);

            string s = Common.Instance.TruncateOneLine(g, person.Name, Common.Instance.Font, maxstringwidth);
            Size size = g.MeasureString(s, Common.Instance.SubFont).ToSize();
            g.DrawString(s,
                          Common.Instance.SmallBoldFont, 
                          fontBrush,
                          Rectangle.Left + (ConnectionListItem.HSpacing + ConnectionListItem.ImageSize), // Right of image
                          Rectangle.Top + 8 );

            s = Common.Instance.TruncateOneLine(g, person.Headline, Common.Instance.SmallFont, maxstringwidth);
            g.DrawString(s, 
                          Common.Instance.SmallFont, 
                          fontBrush,
                          Rectangle.Left + (ConnectionListItem.HSpacing + ConnectionListItem.ImageSize), // Right of image
                          Rectangle.Top + size.Height + 8 + ConnectionListItem.Spacing );
        }
    }
}
